package handler;

import entity.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;

public class NettyClientHandler extends SimpleChannelInboundHandler<RpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RpcResponse rpcResponse) throws Exception {
        try{
            System.out.println("客户端开始处理响应");
            AttributeKey<RpcResponse> key = AttributeKey.valueOf("rpcResponse");

            channelHandlerContext.attr(key).set(rpcResponse);
            channelHandlerContext.channel().close();
        }catch (Exception e){
            throw new Exception("客户端处理响应失败！");
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("客户端处理响应时时有错误发生:");
        cause.printStackTrace();
        ctx.close();
    }
}
